#include "stdafx.h"


#include <QtCore/QCoreApplication>
#include <QTextStream>
#include <QtGui/QFileDialog>
#include <QDataStream>
//#include <QApplication>
#include "volumereader.h" 
#include "Attrs.h"
#include <iostream>
#include "scoped_array.h"

#include <QApplication>



#include "testVR.h"
#include "mainwindow.h"
#include "utils.h"

static QTextStream os(stdout);
static QTextStream is(stdin);


int scanFullDisk() 
{ 
	
	
	/************************************************************************/
	/* TESTING*/
	/************************************************************************/
	// 	testVR tc;
	// 	QTest::qExec(&tc,argc,argv);
	/************************************************************************/
	/* MAIN ROUTINE                                                         */
	/************************************************************************/
	//VolumeReader vr("\\\\.\\i:");
	//VolumeReader vr("c:\\ntfs-con.dd");
	//VolumeReader vr("ntfs-undel-7.dd");
	QString imagePath;
	os<<"Input image path:";os.flush();
	
	is>>imagePath;
	VolumeReader vr(imagePath);
	UINT64 vsize = vr.volumeSize()/vr.clusterSize()*vr.clusterSize();//valid size
// 	for (UINT64 i = 0;i < vsize;i+=1024) {
// 		if (vr.isMFT(i)) {
// 			CMFT cur= vr.MFTAt(i);
// 			if (cur.countAttr(AttrType::AttributeList)) {
// 				break;//cc
// 			}
// 			if (!cur.hasExisted())
// 				continue;
// 			if (!cur.isBase()) {
// 				os<<"NONE BASE MFT "<<i<<endl;
// 				continue;
// 			}
// 			os<<cur.getFileName()
// 				<<" EntryNo:"<<cur.entryNo()
// 				<<" StartByte: "<<i<<endl;
// 		}
// 	}
	// 	printMFTInfo(&vr.MFTEntry(32));
	// 	system("pause");
	while (true) {
		os<<"Input mft start byte:";
		os.flush();
		UINT64 sb;
		std::cin>>sb;
		CMFT mft = vr.MFTAt(sb);
		printMFTInfo(&mft);
		os<<"Input Attr Type:";
		os.flush();
		int typeId;
		is>>typeId;
		os<<"Input ID:";
		os.flush();
		int dataId;
		is>>dataId;
		CAttribute dataAtt = mft.getAttribute(typeId,dataId);

		QString fileName = QFileDialog::getSaveFileName(0,"Recover as",mft.getFileName());
		//really write the file
		if (!fileName.isNull()) {
			QFile outFile(fileName);
			outFile.open(QFile::WriteOnly);
			QDataStream dataStream(&outFile);
			char buffer[1024000];
			UINT64 i;
			for ( i = 0;i<dataAtt.size()/vr.clusterSize();i++) {
				dataAtt.read(i*vr.clusterSize(),vr.clusterSize(),buffer );
				dataStream.writeRawData(buffer ,vr.clusterSize());
			}
			if (dataAtt.size()%vr.clusterSize()!=0) { //< write the tails
				UINT64 start = i*vr.clusterSize();
				dataAtt.read(start,dataAtt.size()-start,buffer );
				dataStream.writeRawData(buffer ,dataAtt.size()-start);
			}
		}
		continue;

	}
	system("pause");
}


void scanMFTArea() 
{
	QString imagePath;
	os<<"Input image path:";os.flush();

	is>>imagePath;
	VolumeReader vr(imagePath);
	CMFT masterMFT = vr.MFTEntry(0);
	printMFTInfo(&masterMFT);
	std::cout<<"input $DATA id:";
	int masterDataId;
	is>>masterDataId;
	CAttribute masterData = masterMFT.getAttribute(AttrType::Data,masterDataId);
	char buffer[1024]; {
		CMFT cur = vr.MFTEntry(164096);
		printMFTInfo(&cur);
	}
//  	for (UINT64 i = 0;i < masterData.size();i+=1024) {
//  		UINT64 diskOffset = masterMFT.getAttPos(AttrType::Data,masterDataId,i);
//  		if(!vr.isMFT(diskOffset)) {
//  			std::cout<<"Not MFT AT entry :"<<i/1024<<"\n";
//  			continue;
//  		}
//  
//  		CMFT cur= vr.MFTAt(diskOffset);
//  		//printMFTInfo(&cur);
// 		if (cur.countAttr(AttrType::AttributeList)) {
// 			std::cout<<"Multi MFT start at"<<diskOffset<<endl;
// 		}
//  		if (!cur.hasExisted())
//  			continue;
//  		
//  		if (!cur.isBase()) {
//  			os<<"NONE BASE MFT "<<diskOffset<<endl;
//  			//printMFTInfo(&cur);
//  			continue;
//  		}
// //  		os<<cur.getFileName()
// //  			<<" EntryNo:"<<cur.entryNo()
// //  			<<" StartByte: "<<diskOffset<<endl;
// 
// 	}

	while (true) {
		os<<"Input mft start byte:";
		os.flush();
		UINT64 sb;
		std::cin>>sb;
		CMFT mft = vr.MFTAt(sb);
		printMFTInfo(&mft);
		os<<"Input Attr Type:";
		os.flush();
		int typeId;
		is>>typeId;
		os<<"Input ID:";
		os.flush();
		int dataId;
		is>>dataId;
		CAttribute dataAtt = mft.getAttribute(typeId,dataId);

		QString fileName = QFileDialog::getSaveFileName(0,"Recover as",mft.getFileName());
		//really write the file
		if (!fileName.isNull()) {
			QFile outFile(fileName);
			outFile.open(QFile::WriteOnly);
			QDataStream dataStream(&outFile);
			scoped_array<char> buffer(new char[vr.clusterSize()]);
			UINT64 i;
			for ( i = 0;i<dataAtt.size()/vr.clusterSize();i++) {
				dataAtt.read(i*vr.clusterSize(),vr.clusterSize(),buffer.get());
				dataStream.writeRawData(buffer.get(),vr.clusterSize());
			}
			if (dataAtt.size()%vr.clusterSize()!=0) { //< write the tails
				UINT64 start = i*vr.clusterSize();
				dataAtt.read(start,dataAtt.size()-start,buffer.get());
				dataStream.writeRawData(buffer.get(),dataAtt.size()-start);
			}
		}
		continue;

	}
}

int main(int argc,char**argv) {
	QApplication app(argc,argv);
// 	VolumeReader vr("\\\\.\\i:");
// 	auto cur = vr.MFTEntry(38);
#ifdef _DEBUG
	/************************************************************************/
	/* TESTING*/
	/************************************************************************/
	//testVR tc;
	//QTest::qExec(&tc,argc,argv);
#endif
 	MainWindow m;
 	m.show();
 	return app.exec();
	//scanMFTArea();
	//scanFullDisk();
}
